$(document).ready(function(){
	//登录页判断显示模块
	var usersign = $("input[name='usersign']").val();
	if(usersign == '1'){
		document.getElementById('loginName').style.display='none';
		document.getElementById('dhloginName').style.display='block';
		//$('.loginName').addClass('display_n').removeClass('display_b');
		//$('.dhloginName').addClass('display_b').removeClass('display_n');
		$(".account_dh").addClass('log_on');
		$(".account_com").removeClass('log_on');
	}else if(usersign == '0'){
		document.getElementById('loginName').style.display='block';
		document.getElementById('dhloginName').style.display='none';
		//$('.loginName').addClass('display_b').removeClass('display_n');
		//$('.dhloginName').addClass('display_n').removeClass('display_b');
		$(".account_com").addClass('log_on');
		$(".account_dh").removeClass('log_on');
	}
	//直聘登录
	$("#dhlogin").bind("click",dhlogin);
	//直聘立即注册跳转
	$("#dh_register").bind("click",dh_register);
	$("#login").click(function(){
		login();
	})
	$("#register").click(function(){
		window.location.href ='register.php';
	})
	$("#verify").css('border-bottom','none');
	
	$(".user_register").click(function(){
		register();
	});

	$("#ctmname").blur(function(){
		if($("#ctmname").val().match(/^1[34578]{1,1}[0-9]{9,9}$/)){
			$("#hrname").val($.trim($("#ctmname").val()));
			$("#hrname").removeClass('default');
			$("#ctmname").attr('value',$("#ctmname").val())
		}	
	})

	$("#changeVerify,#dh_changeVerify").click(function(){
		var url = '../ajax/changeverify.ajax.php';
		var result = $.ajax({
			url:url,
			asyn:false,
			dataType:'json',
			type:'post',
			success:function(result){
				if(result.status == 1){
					$("#changeVerify,#dh_changeVerify").prev().attr("src",'data:image/png;base64,'+result.desc);
				}
			}
		});
	})
	$("#barea").change(function(){
		changeArea();
	})
	$("#area").change(function(){
		if($("#area").prev().html() != '请选择城市'){
			$("#area").prev().removeClass('c_gray');
		}else{
			$("#area").prev().addClass('c_gray');
		}
	})
	$("#registerback").click(function(){
		if(document.referrer == ''){
			location.href = 'login.php';
		}else{
			location.href=document.referrer;
		}
	})
})
function findpass(){
	var val = $("#ctmname").attr('value');
	if(val != '请输入会员名' && val.match(/^1[34578]{1,1}[0-9]{9,9}$/)){
		location.href = 'findpwd.php?phone='+val;
	}else{
		location.href = 'findpwd.php';
	}
	
}
function register(){
	//解决线上频繁点击注册按钮，导致多次注册提交问题
	$(".user_register").attr('disabled',true);
	$(".user_register").addClass("but_gray").removeClass("but");
	var companyname = $("#companyname").val();
		areacode    = $("#area").val(),
		address    	= $("#address").val(),
		username 	= $("#username").val(),
		telephone 	= $("#telephone").val(),
		email 	    = $("#email").val(),
		verify		= $('#verify_code').val();
	if('请输入公司名' == companyname){
		pop('请输入公司名',1,'确定','','popclose()');
		click_register();
		return false;
	}
	var companylen = companyname.replace(/[^\x00-\xff]/g,"**").length;
	if (companylen > 100){
		pop('公司名过长！',1,'确定','','popclose()');
		click_register();
		return false;
	}
	if('请选择城市' == areacode){
		pop('请选择城市',1,'确定','','popclose()');
		click_register();
		return false;
	}
	if('请输入地址' == address){
		pop('请输入地址',1,'确定','','popclose()');
		click_register();
		return false;
	}
	var addresslen = address.replace(/[^\x00-\xff]/g,"**").length;
	if (addresslen > 200){
		pop('地址过长！',1,'确定','','popclose()');
		click_register();
		return false;
	}
	if('请输入联系人' == username){
		pop('请输入联系人',1,'确定','','popclose()');
		click_register();
		return false;
	}
	var usernamelen = username.replace(/[^\x00-\xff]/g,"**").length;
	if (usernamelen > 100){
		pop('联系人过长！',1,'确定','','popclose()');
		click_register();
		return false;
	}
	if('请输入联系电话' == telephone){
		pop('请输入联系电话',1,'确定','','popclose()');
		click_register();
		return false;
	}
	var telephonelen = telephone.replace(/[^\x00-\xff]/g,"**").length;
	if (telephonelen > 20){
		pop('联系电话过长！',1,'确定','','popclose()');
		click_register();
		return false;
	}
	if('请输入Email' == email){
		pop('请输入Email',1,'确定','','popclose()');
		click_register();
		return false;
	}
	if(!/^(([0-9a-zA-Z]+)|([0-9a-zA-Z]+[_.0-9a-zA-Z-]*))@([A-Za-z0-9]+[-A-Za-z0-9_]*\.)+[A-Za-z]{2,}$/.test(email))
	{
		pop('对不起，您Email输入有误',1,'确定','','popclose()');
		click_register();
		return false;
	}
	var emaillen = email.replace(/[^\x00-\xff]/g,"**").length;
	if (emaillen > 80){
		pop('Email过长！',1,'确定','','popclose()');
		click_register();
		return false;
	}
	if('请输入验证码' == verify || '' == verify){
		pop('请输入验证码',1,'确定','','popclose()');
		click_register();
		return false;
	}
	
	var url = '../ajax/user/register.ajax.php';
	var param = {'companyname':companyname,
				 'username':username,
				 'areacode':areacode,
				 'address':address,
				 'email':email,
				 'verify':verify,
				 'telephone':telephone};
	var result = $.ajax({
		url:url,
		asyn:false,
		dataType:'json',
		type:'post',
		data:param,
		timeout:60000, //ajax请求超时60s
		success:function(result){
			if(result.status == 1){
				location.href = result.url;
			}else if(result.status == 4){
				pop('验证码错误',1,'确定','','popclose()');
				$('#verify_code').val('');
				$("#changeVerify").click();
				return false;
			}else{
				pop(result.desc,1,'确定','','popclose()');
				return false;
			}
		},
		error:function(){ 
			pop("请求超时，请重新提交注册信息！",1,'确定','','popclose()');
		},
		complete:function(){
			click_register();
		}
	});
}
//注册移除disabled增加onclick事件
function click_register()
{	
	$(".user_register").addClass("but").removeClass("but_gray");
	$('.user_register').removeAttr("disabled");
}

function login(){
	var ctmname 	= $('#ctmname').val(),
		hrname 		= $('#hrname').val(),
		password 	= $('#password').val(),
		verify		= $('#verify').val(),
		//autoLogin   = $('#autoLogin').find('font').attr('class'),
		isshow		= $('#show_verify').css('display');
	if('请输入会员名' == ctmname){
		pop('请输入会员名','1','确定','','popclose()');
		return false;
	}
	if('请输入用户名' == hrname){
		pop('请输入用户名','1','确定','','popclose()');
		return false;
	}
	if('请输入密码' == password || '' == password){
		pop('请输入密码','1','确定','','popclose()');
		return false;
	}
	if('block' == isshow){
		if('请输入验证码' == verify){
			pop('请输入验证码','1','确定','','popclose()');
			return false;
		}
	}	

	var url = '../ajax/user/login.ajax.php';
	var param = {'ctmname'	:ctmname,
				 'hrname' 	:hrname,
				 'password'	:password,
				 'verify'	:verify,
				 'isshow'	:isshow,
				 'sourcepage':'login'};
	var result = $.ajax({
		url:url,
		asyn:false,
		dataType:'json',
		type:'post',
		data:param,
		success:function(retDate){
			if(retDate.status == 1){				
				window.location.href = retDate.url;
			}else{
				if(retDate.status == 3){
					$("#show_verify").css("display","block");
				}
				if(retDate.status == 4){
					pop('验证码错误',1,'确定','','popclose()');
					$('#verify').val('');
					$("#changeVerify").click();
					return false;
				}
				pop(retDate.desc,1,'确定','','popclose()');
				$('#verify').val('');
				$("#changeVerify").click();
				return false;
			}
			
		}
	});
}

function change(){
	var param = {'action'	:'verify'};
	$.ajax({
		url:'../ajax/user/findpwd.ajax.php',
		dataType:'json',
		type:'post',
		data:param,
		success:function(result){
			if(result.status ==1){
				$("#verify").attr('src','data:image/png;base64,'+result.desc);
			}	
		}
	});
}

function findpwd(){
	//解决频繁点击按钮，导致多次提交问题
	$(".addfindpwd").removeAttr('onclick');
	$(".addfindpwd").addClass('but_gray').removeClass('but');
	var ctmname = $('#ctmname').val(),
		hrname 	= $('#hrname').val(),
		EMail 	= $('#EMail').val();
	if('请输入会员名' == ctmname && '请输入用户名' == hrname && '请输入EMail' == EMail){
		pop('会员名,用户名和EMail均不能为空！',1,'确定','','popclose()');
		findpwd_click();
		return false;
	}
	if('请输入会员名' == ctmname){
		pop('请输入会员名',1,'确定','','popclose()');
		findpwd_click();
		return false;
	}
	if('请输入用户名' == hrname){
		pop('请输入用户名',1,'确定','','popclose()');
		findpwd_click();
		return false;
	}
	if('请输入EMail' == EMail){
		pop('请输入EMail',1,'确定','','popclose()');
		findpwd_click();
		return false;
	}
	var ctmnamelen = ctmname.replace(/[^\x00-\xff]/g,"**").length;
	if (ctmnamelen > 20){
		pop('会员名过长',1,'确定','','popclose()');
		findpwd_click();
		return false;
	}
	var hrnamelen = hrname.replace(/[^\x00-\xff]/g,"**").length;
	if (hrnamelen > 20){
		pop('用户名过长',1,'确定','','popclose()');
		findpwd_click();
		return false;
	}
	var EMaillen = EMail.replace(/[^\x00-\xff]/g,"**").length;
	if (EMaillen > 80){
		pop('EMail过长',1,'确定','','popclose()');
		findpwd_click();
		return false;
	}
	if(!/^(([0-9a-zA-Z]+)|([0-9a-zA-Z]+[_.0-9a-zA-Z-]*))@([A-Za-z0-9]+[-A-Za-z0-9_]*\.)+[A-Za-z]{2,}$/.test(EMail))
	{
		pop('对不起，您Email输入有误',1,'确定','','popclose()');
		findpwd_click();
		return false;
	}
	var url = '../ajax/user/findpwd.ajax.php';
	var param = {'ctmname':ctmname,
				 'hrname':hrname,
				 'EMail':EMail,
				 'action':'submit'};
	var result = $.ajax({
		url:url,
		asyn:false,
		dataType:'json',
		type:'post',
		data:param,
		timeout:60000, //ajax请求超时60s
		success:function(result){
			if(result.status == 1){
				pop(result.desc,1,'确定','','popjump()');
				$("input[name='back_login_url']").val(result.back_login_url);
				return true;
			}else{
				pop(result.desc,1,'确定','','popclose()');
				return false;
			}
		},
		error:function(){ 
			pop("请求超时，请重新提交！",1,'确定','','popclose()');
		},
		complete:function(){
			findpwd_click();
		}
	});
}
//找回密码绑定onclick
function findpwd_click(){
	$(".addfindpwd").attr('onclick','findpwd()');
	$(".addfindpwd").addClass('but').removeClass('but_gray');
}

//忘记密码确定后跳转路径
function popjump(){
	popclose();
	var login_url = $("input[name='back_login_url']").val();
	window.location.href = ''+login_url+'';
}

function resetpwd(){
	//解决频繁点击按钮，导致多次提交问题
	$(".adduser_resetpwd").removeAttr('onclick');
	$(".adduser_resetpwd").addClass('but_gray').removeClass('but');
	var newpwd = $("#newpwd").val();
	var checkpwd = $("#checkpwd").val();
	//过滤前后空格
	newpwd   = newpwd.replace(/^[\s]+|[\s]+$/g,"").replace(/^[<]+|[<]+$/g,"").replace(/^[>]+|[>]+$/g,"");
	checkpwd = checkpwd.replace(/^[\s]+|[\s]+$/g,"").replace(/^[<]+|[<]+$/g,"").replace(/^[>]+|[>]+$/g,"");
	var url = '../ajax/user/resetpwd.ajax.php';
	var param = {'newpwd':newpwd,'checkpwd':checkpwd};
	var result = $.ajax({
		url:url,
		asyn:false,
		dataType:'json',
		type:'post',
		data:param,
		timeout:60000, //ajax请求超时60s
		success:function(result){
			if(result.status == 1){
				location.href = result.url;
			}else{
				pop(result.desc,1,'确定','','popclose()');
				return false;
			}
		},
		error:function(){ 
			pop("请求超时，请重新提交！",1,'确定','','popclose()');
		},
		complete:function(){
			resetpwd_click();
		}
	});
}
//企业版修改密码绑定onclick事件
function resetpwd_click(){
	$(".adduser_resetpwd").attr('onclick','resetpwd()');
	$(".adduser_resetpwd").addClass('but').removeClass('but_gray');
}

function sendphonecode(){
	if($('#timeBut').hasClass('gray')){
		return;
	}
	var telphone = $('#telphone').val();
	var code 	 = $("#verify_code").val();
	if('请输入手机号' == telphone){
		pop('请输入手机号',1,'确定','','popclose()');
		return false;
	}
	if('请输入验证码' == code){
		pop('请输入验证码',1,'确定','','popclose()');
		return false;
	}
	var param = {'telphone':telphone,
				 'code':code,
			 	 'type':'1',
	}

	var result = $.ajax({
			url:'../ajax/sendphonecode.ajax.php',
			asyn:false,
			dataType:'json',
			type:'post',
			data:param,
			success:function(result){
				if(result.status == 1){	
					$('#timeBut').html('60秒后重新发送');
					$('#timeBut').addClass('gray');
					timeClock('timeBut');
					return true;
				}else if(result.status == 2){
					$("#verifypic").attr('src','data:image/png;base64,'+result.pic)
					pop(result.desc,1,'确定','','popclose()');
				}else{
					pop(result.desc,1,'确定','','popclose()');
				}
			}
		});
}
function changeArea(){
	var barea = $('#barea').val();
	var htm = '';
	var content = '';
	content = '请选择城市';
	for(var ele in area_c){
		if((area_c[ele].substr(0,2) == '01' || area_c[ele].substr(0,2) == '02' || area_c[ele].substr(0,2) == '04' || area_c[ele].substr(0,2) == '05' || area_c[ele].substr(0,2)== '06' || area_c[ele].substr(0,2) =='33' || area_c[ele].substr(0,2) == '34' || area_c[ele].substr(0,2) == '35' || area_c[ele].substr(0,2) == '36') && barea == area_c[ele]){
			htm = '<option value="' + area_c[ele] + '">' + area_v[ele] + '</option>';
			content = area_v[ele];
		}else if((area_c[ele].substr(0,2) == barea.substr(0,2)) && barea == area_c[ele] && area_c[ele].substr(2,4) == '0000'){
			htm += '<option>请选择城市</option>';
		}else if(area_c[ele].substr(0,2) == barea.substr(0,2)){
			htm += '<option value="' + area_c[ele] + '">' + area_v[ele] + '</option>';
		}
	}
	if($("#barea").prev().html() != '请选择省份'){
		$("#barea").prev().removeClass('c_gray');
		$("#area").prev().addClass('c_gray');
	}else{
		$("#barea").prev().addClass('c_gray');
	}
	if('' == htm){
		htm = '<option>请选择城市</option>';
		$("#area").prev().addClass('c_gray');
	}
	if(content == '上海'|| content == '北京'|| content == '深圳'|| content == '重庆'|| content == '天津'|| content == '香港'|| content == '澳门'|| content == '台湾'|| content == '国外'){
		$("#area").prev().removeClass('c_gray');
	}
	$('#area').prev().html(content);
	$('#area').html(htm);
}
$('.bb select ').change(function(){//下拉框取值
	var v = '';
	$(this).find('option').each(function(){
		if($(this).attr('selected') == true){
			v = $(this).html();
			return false;
		}
	});
	$(this).siblings().html(v);
}); 

//直聘账号
function login_dh()
{
	document.getElementById('loginName').style.display='none';
	document.getElementById('dhloginName').style.display='block';
	//$('.loginName').addClass('display_n').removeClass('display_b');
	//$('.dhloginName').addClass('display_b').removeClass('display_n');
	$(".account_dh").addClass('log_on');
	$(".account_com").removeClass('log_on');
}
//企业账号
function login_com()
{
	document.getElementById('dhloginName').style.display='none';
	document.getElementById('loginName').style.display='block';
	//$('.loginName').addClass('display_b').removeClass('display_n');
	//$('.dhloginName').addClass('display_n').removeClass('display_b');
	$(".account_com").addClass('log_on');
	$(".account_dh").removeClass('log_on');
}
//登录
function dhlogin()
{
	var phone 	 = $('#phone').val(),
		password = $('#dh_password').val(),
		verify	 = $('#dh_verify').val(),
		isshow	 = $('#show_dhverify').css('display');
	if('请输入手机号码' == phone || '' == phone){
		pop('请输入手机号码！','1','确定','','popclose()');
		return false;
	}
	if('请输入密码' == password || '' == password){
		pop('请输入密码','1','确定','','popclose()');
		return false;
	}
	if('block' == isshow){
		if('请输入验证码' == verify){
			pop('请输入验证码','1','确定','','popclose()');
			return false;
		}
	}	

	var url = '../ajax/user/login.ajax.php';
	var param = {'phone' 	:phone,
				 'password'	:password,
				 'verify'	:verify,
				 'sourcepage' :'dhlogin',
				 'isshow'	:isshow};
	var result = $.ajax({
		url:url,
		asyn:false,
		dataType:'json',
		type:'post',
		data:param,
		success:function(retDate){
			if(retDate.status == 1){				
				window.location.href = retDate.url;
			}else{
				if(retDate.status == 3){
					$("#show_dhverify").css("display","block");
				}
				if(retDate.status == 4){
					pop('验证码错误',1,'确定','','popclose()');
					$('#dh_verify').val('');
					$("#dh_changeVerify").click();
					return false;
				}
				pop(retDate.desc,1,'确定','','popclose()');
				$('#dh_verify').val('');
				$("#dh_changeVerify").click();
				return false;
			}
			
		}
	});
}
//直聘-找回密码页
function dh_findpass()
{
	window.location.href="../dhuser/dhfindpwd.php";
}
//直聘-立即注册跳转
function dh_register()
{
	window.location.href="../dhuser/dhregister.php";
}
